package com.cjb.cloudsystem.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjb.cloudsystem.entity.SysMsg;
import com.cjb.cloudsystem.service.SysMsgService;
import com.cjb.cloudsystem.vo.SearchMsgVO;
import com.cjb.cloudsystem.vo.SysMsgVO;
import com.cjb.common.common.cache.RedisCache;
import com.cjb.common.util.Common;
import com.cjb.common.util.JsonUtils;
import com.cjb.common.util.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @Author 1.0.0 2024-03-15
 * @Package IntelliJ IDEA
 * @Class SysMsgControl
 * @Date 2024-03-15
 */
@RestController
@Tag(name = "sysMsg")
@RequestMapping(value = {"sysMsg"})
@ApiResponses(@ApiResponse(responseCode = "200", description = "接口请求成功"))
@Slf4j
public class SysMsgController {
    /*
     * 注入事物类
     */
    @Autowired
    private SysMsgService sysMsgService;
    /**
     * 注入Redis服务类
     */
    @Autowired
    private RedisCache redisCache;

    /**
     * 单个保存
     *
     * @Author 1.0.0 2024-03-15
     * @Value
     * @Title
     */
    @PostMapping("/save")
    @Operation(summary = "单个保存")
    public Result<Boolean> save(@RequestBody SysMsg sysMsg) {
        sysMsg.setDeleted(0);
        if (Common.isEmpty(sysMsg.getMsgSendTime())){
            sysMsg.setMsgSendTime(new Date());
        }
        if (Common.isEmpty(sysMsg.getMsgContent())){
            sysMsg.setMsgContent(UUID.randomUUID().toString());
        }
        return Result.success(sysMsgService.save(sysMsg));
    }

    /**
     * 批量保存
     *
     * @Author 1.0.0 2024-03-15
     * @Value
     * @Title
     */
    @PostMapping("/listSave")
    @Operation(summary = "批量保存")
    public Result<Integer> listSave(@RequestBody List<SysMsg> sysMsg) {
        return Result.success(sysMsgService.listSave(sysMsg));
    }

    /**
     * 单个查询
     *
     * @Author 1.0.0 2024-03-15
     * @Value
     * @Title
     */
    @GetMapping("{id}")
    @Operation(summary = "单个查询-通过ID")
    public Result<SysMsg> queryById(@PathVariable("id") Integer id) {
        SysMsg sysMsg = new SysMsg();
        sysMsg.setMsgId(id);
        return Result.success(sysMsgService.queryById(sysMsg));
    }

    /**
     * 分页查询
     *
     * @Author 1.0.0 2024-03-15
     * @Value
     * @Title
     */
    @GetMapping("/queryPage")
    @Operation(summary = "分页查询-通过页码")
    public Result<Page<SysMsg>> queryPage(@ParameterObject @Valid SysMsgVO sysMsgVO) {
        Page sysMsgPage = new Page<SysMsg>(sysMsgVO.getPage(), sysMsgVO.getLimit());
        return Result.success(sysMsgService.queryPage(sysMsgPage, sysMsgVO));
    }

    /**
     * 批量更新
     *
     * @Author 1.0.0 2024-03-15
     * @Value
     * @Title
     */
    @PostMapping("/listUpdate")
    @Operation(summary = "批量更新")
    public Result<String> listUpdate(@RequestBody List<SysMsg> sysMsg) {
        sysMsgService.listUpdate(sysMsg);
        return Result.success();
    }


    /**
     * 批量删除
     *
     * @Author 1.0.0 2024-03-15
     * @Value
     * @Title
     */
    @PostMapping("/listDelete")
    @Operation(summary = "批量删除")
    public Result<String> listDelete(@RequestBody List<SysMsg> sysMsg) {
        sysMsgService.listDelete(sysMsg);
        return Result.success();
    }

    /**
     * 通过id删除
     *
     * @Author 1.0.0 2024-03-15
     * @Value
     * @Title
     */
    @PostMapping("/deleteById")
    @Operation(summary = "单个删除-通过ID")
    public Result<String> deleteById(@RequestBody SysMsg sysMsg) {
        log.info("进入操作 {}", JsonUtils.toJsonString(sysMsg));
        sysMsgService.deleteById(sysMsg);
        return Result.success();
    }

    /**
     * 返回全部信息
     *
     * @Author 1.0.0 2024-03-15
     * @Value
     * @Title
     */
    @PostMapping("/returnAllData")
    @Operation(summary = "返回所有信息")
    public Result<List<SysMsg>> returnAllData() {
        return Result.success(sysMsgService.returnAllData());
    }

    /**
     * 查询订阅藏品
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @PostMapping("/searchArt")
    @Operation(summary = "返回所有信息")
    public Result<SearchMsgVO> searchArt(String goodName) {
        return Result.success(sysMsgService.searchArt(goodName));
    }

    /**
     * 订阅/取消操作
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @PostMapping("/subAndCancel")
    @Operation(summary = "返回所有信息")
    public Result<SearchMsgVO> subAndCancel(@RequestBody SysMsgVO sysMsgVO) throws Throwable {
        if (Common.isEmpty(sysMsgVO.getMsgContent()) || sysMsgVO.getMsgContent().equals("")){
            log.info("生成随机数{}");
            sysMsgVO.setMsgContent(UUID.randomUUID().toString());
        }
        // 查找操作
        log.info("打印传递消息{}",JsonUtils.toJsonString(sysMsgVO));
        if (Common.isNotEmpty(sysMsgVO) && Common.isNotEmpty(sysMsgVO.getOperate())
        && Common.isNotEmpty(sysMsgVO.getSubRule()) && Common.isNotEmpty(sysMsgVO.getMsgContent())) {
            //保存信息到数据库
            SysMsg sysMsg = new SysMsg();
            sysMsg.setDeleted(0);
            Common.copyNonNullProperties(sysMsgVO, sysMsg);
            if (Common.isNotEmpty(sysMsg.getMsgId())){
                   //更新
                List<SysMsg> sysMsgs = new ArrayList<>();
                sysMsgs.add(sysMsg);
                sysMsgService.listUpdate(sysMsgs);
            }
            else {
                sysMsgService.save(sysMsg);
            }
        }
        return Result.success();
    }

}
